﻿//#define EmployeeDeBug //employee 测试

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

using TRPOP.Common;
using TRPOP.CRM.Component.Model;
using TRPOP.CRM.Component.Business;

namespace TRPOP.CRM.Component.Common
{
    /// <summary>
    /// 所有的后台页面所需要继承的基本类
    /// </summary>
    public class BaseAdmin : BasePage
    {
        #region override OnInit

        /// <summary>
        /// 重载 OnInit 验证用户权限
        /// </summary>
        protected override void OnInit(EventArgs e)
        {
            if (!this.DesignMode)
            {
                CheckPermission();
            }

            base.OnInit(e);
        }

        #endregion

        #region  用户的登录验证 权限验证

        protected void CheckPermission()
        {
            //如果是登陆页面，则无需做登陆验证
            if (RequestHelper.GetCurrentUrl().ToLower().IndexOf("login.aspx") > 0)
            {
                return;
            }

            // 如果用户都没登陆，则直接跳转至login.aspx页面
            if (!IsEmployeeLogin)
            {
                base.ExecuteJsWidthEnd("parent.document.location = '/login.aspx'");
                return;
            }

            // 子页面初始化
            SubPageInit();

            if (m_oCurrentPermissionKey.IsEmpty() == false)
            {
                if (m_oCurrentPermissionInfos.Find(oInfo => oInfo.nvcPermissionCode.Trim().ToLower() == m_oCurrentPermissionKey.Trim().ToLower()) == null)
                {
                    base.Alert("您不具备访问当前页面的权限！", "/welcome.aspx");
                    return;
                }
            }
        }

        public bool CheckLogin()
        {
#if EmployeeDeBug
            return true;
#else
            return Session[SystemConst.EmployeeSessionKey] != null;
#endif
        }

        /// <summary>
        /// 当前页面的权限配置信息
        /// </summary>
        protected List<PermissionInfo> m_oCurrentPermissionInfos
        {
            get
            {
                if (Session[SystemConst.EmployeeSessionPermissiobKey] == null)
                {
                    EmployeeInfo oEmployeeInfo = GetCurrentEmployeeInfo();
                    if (oEmployeeInfo.nvcEmployeeName == "admin")
                    {
                        Session[SystemConst.EmployeeSessionPermissiobKey] = PermissionManage.GetAllPermissionInfo();
                    }
                    else
                    {
                        Session[SystemConst.EmployeeSessionPermissiobKey] = PermissionManage.GetPermissionInfoByEmployeeID(oEmployeeInfo.iEmployeeID);
                    }
                }

                return Session[SystemConst.EmployeeSessionPermissiobKey] as List<PermissionInfo>;
            }
        }
        protected string m_oCurrentPermissionKey = string.Empty;

        /// <summary>
        /// 子页面初始化 有权限限制的，请在继承的子页面里重写此方法，以便限制权限
        /// </summary>
        protected virtual void SubPageInit()
        {

        }

        #endregion

        #region 获取当前用户信息

        /// <summary>
        /// 当前是否Employee登录
        /// </summary>
        protected bool IsEmployeeLogin
        {
            get
            {
                return CheckEmployeeLogin();
            }
        }

        #region Employee Login

        // Employee Login
        protected bool EmployeeLogin(string strUsername, string strPWD)
        {
            if (strUsername.IsEmpty() || strPWD.IsEmpty())
            {
                //登录信息不完整，登陆失败
                return false;
            }

            EmployeeInfo oEmployeeInfo = EmployeeManage.CheckLogin(strUsername, strPWD);

            if (oEmployeeInfo != null)
            {
                //保存在Session中
                System.Web.HttpContext.Current.Session[SystemConst.EmployeeSessionKey] = oEmployeeInfo;
                return true;
            }

            return false;
        }

        // Get Current EmployeeInfo
        protected EmployeeInfo GetCurrentEmployeeInfo()
        {
            EmployeeInfo oEmployeeInfo = null;

#if EmployeeDeBug
            oEmployeeInfo = EmployeeManage.GetAdminEmployeeInfo();
#else
            if (Session[SystemConst.EmployeeSessionKey] != null)
                oEmployeeInfo = Session[SystemConst.EmployeeSessionKey] as EmployeeInfo;
#endif

            return oEmployeeInfo;

        }

        // Check if Employee Logined
        protected bool CheckEmployeeLogin()
        {

#if EmployeeDeBug
            return true;
#else
            return Session[SystemConst.EmployeeSessionKey] != null;
#endif

        }


        #endregion

        #endregion

        #region override OnError

        protected override void OnError(EventArgs e)
        {
            // 捕获异常，将错误记录至数据库
            SystemLogInfo oInfo = new SystemLogInfo();

            oInfo.nvcDiscription = Server.GetLastError().Message;
            oInfo.dtCreationTime = DateTime.Now;
            oInfo.iEmployeeID = 1;
            oInfo.iOperationKey = (int)SystemConst.SystemOperationKey.SystemError;

            BaseManage.SaveSystemLog(oInfo);

            base.OnError(e);
        }

        #endregion
    }
}
